﻿/**
* @author Alexander Rindo
* @project Flashview
* @version 0.4
* @purpose Class for the zoomSlider Movieclip
**/

package com.alexrindo.flashview.UI 
{
	import com.alexrindo.flashview.image.ImageManager;
	import flash.display.Sprite;
	import flash.display.Stage;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	
	
	public class zoomSlider extends Sprite
	{
		private const START_X:int = 31
		private const START_Y:int = -135
		private const WIDTH:int = 0
		private const HEIGHT:int = 235
		
		private var _bounds:Rectangle
		private var _stage:Stage
		private var _images:ImageManager
		private var _newScale:Number
		private var _currentPosition:Point
		private var _currentZoomLevel:int
		
		public function zoomSlider(stageObject:Stage) 
		{
			_stage = stageObject
			_images = ImageManager.getInstance()
			init()
		}
		
		private function init():void
		{
			setupBounds()
			setupSlider()
			addListeners()
		}
		
		private function setupBounds():void
		{
			_bounds = new Rectangle(START_X,START_Y,WIDTH,HEIGHT)
		}
		
		private function setupSlider():void
		{
			sliderButton.buttonMode = true
			sliderButton.useHandCursor = true
			
			setSliderText()
		}
		
		private function addListeners():void
		{
			sliderButton.addEventListener(MouseEvent.MOUSE_DOWN, onSliderDown)
			sliderBar.addEventListener(MouseEvent.CLICK, onSliderBarClick)
			resetZoom.addEventListener(MouseEvent.CLICK, onResetClick)
			_images.addEventListener(ImageManager.IMAGE_ZOOM, onScaleChange)
			_stage.addEventListener(MouseEvent.MOUSE_UP, onSliderUp)
			addEventListener(MouseEvent.MOUSE_WHEEL, onSliderWheel)
		}
		
		private function onSliderDown(e:MouseEvent):void 
		{
			sliderButton.startDrag(false, _bounds)
			_stage.addEventListener(MouseEvent.MOUSE_MOVE, onSliderMove)
		}
		
		private function onSliderMove(e:MouseEvent):void 
		{
			_currentPosition = localToGlobal(new Point(mouseX, mouseY))
			
			if (hitTestPoint(_currentPosition.x, _currentPosition.y, true))
			{
				_newScale = 2.5 - ((sliderButton.y - START_Y) / (HEIGHT / 2))
				_images.zoom(_newScale, false)
			} else {
				_stage.removeEventListener(MouseEvent.MOUSE_MOVE, onSliderMove)
				sliderButton.stopDrag()
			}
			setSliderText()
		}
		
		private function onSliderUp(e:MouseEvent):void 
		{
			sliderButton.stopDrag()
			_stage.removeEventListener(MouseEvent.MOUSE_MOVE, onSliderMove)
		}
		
		private function onSliderWheel(e:MouseEvent):void
		{
			_images.zoom(e.delta / 50, true)
			setSliderPosition(_images.getImage(_images.index).scaleX)
		}
		
		private function onSliderBarClick(e:MouseEvent):void 
		{
			_currentPosition = new Point(mouseX, mouseY)
			_newScale = 2.5 - (((_currentPosition.y - sliderButton.height/2) - START_Y) / (HEIGHT / 2))
			_images.zoom(_newScale, false)
			setSliderPosition(_newScale)
		}
		
		private function onScaleChange(e:Event):void
		{
			setSliderPosition(_images.getCurrentScale())
		}
		
		private function onResetClick(e:MouseEvent):void
		{
			setSliderPosition(1)
			_images.zoom(1, false)
		}
		
		public function setSliderPosition(position:Number):void
		{
			sliderButton.y = ((2.5 - position) * HEIGHT) / 2 + START_Y
			setSliderText()
		}
		
		private function setSliderText():void
		{
			_currentZoomLevel = (2.5 - ((sliderButton.y - START_Y) / (HEIGHT / 2)))*100
			sliderText.text = _currentZoomLevel.toString() + "%"
		}
		
	}

}